<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <script src="../layui/layui.js"></script>
</head>
<body th:fragment="upload">
    <div style="padding: 10px 10px">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <label for="select" class="layui-form-label">上传目录</label>
                <div class="layui-input-block">
                    <select name="path" id="select" lay-filter="selectpath">
                        <th:block th:each="md : ${mdpath}">
                            <option th:value="${md}" th:text="${md}">写作</option>
                        </th:block>
                    </select>
                </div>
            </div>
            <div class="file-upload">
                <input type="file" name="mdfile" lay-type="file" class="layui-upload-file">
            </div>
        </form>
    </div>

<script>
    layui.use(['upload','form'], function(){
        var form = layui.form();
        var $ = layui.jquery;
        form.render('select'); //刷新select选择框渲染
        //监听select
        form.on('select(selectpath)', function(data){
            removeCookie('filepath');
            addCookie('filepath',data.value);
        });

        layui.upload({
            elem:'.layui-upload-file',
            url: '/upload',
            ext:'md|MD'
            ,success: function(res){
                if (res.status == 0){
                    layui.layer.msg(res.msg);
                    setTimeout('fresh()',1000);
                }else {
                    layui.layer.alert(res.msg);
                }
            }
        });
    });

    function fresh() {
        var $ = layui.jquery;
        $('.fresh-tree button').click();
    }
    // 添加Cookie
    function addCookie(name, value, options) {
        if (arguments.length > 1 && name != null) {
            if (options == null) {
                options = {};
            }
            if (value == null) {
                options.expires = -1;
            }
            if (typeof options.expires == 'number') {
                var time = options.expires;
                var expires = options.expires = new Date();
                expires.setTime(expires.getTime() + time * 1000);
            }
            if (options.path == null) {
                options.path = '/';
            }
            if (options.domain == null) {
                options.domain = '';
            }
            document.cookie = encodeURIComponent(String(name)) + '=' + encodeURIComponent(String(value)) + (options.expires != null ? '; expires=' + options.expires.toUTCString() : '') + (options.path != '' ? '; path=' + options.path : '') + (options.domain != '' ? '; domain=' + options.domain : '') + (options.secure != null ? '; secure' : '');
        }
    }

    // 移除Cookie
    function removeCookie(name, options) {
        addCookie(name, null, options);
    }
</script>
</body>
</html>